<?php

namespace Frieza\Modules\SearchAnalyze\Controllers;

use App\Admin\Extensions\Column\LinkSelect;
use App\Admin\Extensions\Tools\PreviewButton;
use App\Admin\Extensions\Tools\UserButton;
use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Biz\model\activity\Page as mPage;
use Biz\model\activity\PageModule;
use Biz\model\activity\PageModule as mPageModule;
use Biz\model\activity\PageModuleArticle as mPageModuleArticle;
use Biz\model\activity\PageModuleGoods as mPageModuleGoods;
use Biz\model\activity\PageModuleH5Obj as mPageModuleH5Obj;
use Biz\model\activity\PageModuleImg as mPageModuleImg;
use Biz\model\activity\PageModuleWiki as mPageModuleWiki;
use Biz\service\activity\ActivityPage;
use Biz\service\activity\Page;
use Biz\service\activity\Page as sPage;
use Biz\tools\BizUrl;
use Doraemon\model\AbTest\AbTestBasicInfo;
use Doraemon\model\AbTest\AbTestGroupInfo;
use Doraemon\model\Article as mArticle;
use Doraemon\model\Guide;
use Doraemon\model\Member;
use Doraemon\model\TopicActivity;
use Doraemon\model\TopicTab;
use Doraemon\pockets\constInterface\LinkConf;
use Doraemon\service\Blank;
use Doraemon\service\Content\AdmContent;
use Doraemon\service\SearchOp\Schedule;
use Doraemon\service\TopicTab as sTopicTab;
use Doraemon\service\User;
use Doraemon\tools\BlockType;
use Doraemon\tools\BlockTypeMapping;
use Doraemon\tools\IdParse;
use Doraemon\tools\Log as tLog;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

class SearchAnalyzeController extends Controller
{
    public function index(Request $request, Content $content)
    {
        # 获取所有实验名称
        $AbTestBasicWhere = array(
            "test_status" => AbTestBasicInfo::STATUS_NORMAL
        );
        $abTestBasicInfos = AbTestBasicInfo::getList($AbTestBasicWhere, 1000);
        $formatAbTestBasicInfos = array();
        $testBasicIds = array();

        foreach ($abTestBasicInfos as $abTestBasicInfo) {
            $formatAbTestBasicInfos[$abTestBasicInfo["test_id"]] = $abTestBasicInfo["title"];
            array_push($testBasicIds,  $abTestBasicInfo["test_id"]);
        }

        # 获取所有实验下的分组情况
        $AbTestGroupInfos = AbTestGroupInfo::getGroupListByTestIds($testBasicIds, [
            AbTestGroupInfo::NORMAL_STATUS,
            AbTestGroupInfo::RELEASE_STATUS,
            AbTestGroupInfo::CLOSE_STATUS,
        ]);

        $formatTestGroupInfos = array();
        foreach ($AbTestGroupInfos as $AbTestGroupInfo) {
            $formatTestGroupInfos[$AbTestGroupInfo["test_id"]][$AbTestGroupInfo["group_unique_mark"]] = $AbTestGroupInfo["group_unique_mark"];
        }

        $content
            ->header('搜索分析')
            ->description(' ');

        $form = new Form();

        $form->text('keyword', '搜索词')->help('<span style="color:red;">必填</span> 请输入关键词');
        $test = $form->select('ab_test_title', 'ab 实验名称')->options($formatAbTestBasicInfos)->help('<span style="color:red;">必填</span> 选择一个实验名');
        foreach ($formatTestGroupInfos as $testId => $formatTestGroupInfo) {
            $test->when($testId, function (Form $form)use($formatTestGroupInfo){
                $form->select('group_name', '分组名')->options($formatTestGroupInfo);
            });
        }

        $form->disableReset();
        $form->disableSubmit();

        $form->html(<<<EOF
<div id="group_name" class="input-group"></div>
<div id="ab_test_id" class="input-group"></div>
<!--<input name = "ab_test_title" id="ab_test_title" type="hidden">-->
<div  class ="form-group mobile_template">
            <label class="control-label">移动端链接预览 <span style="color:red;">预览时会生成当前请求的url</span></label>
            <div class="input-group">
                <input class="form-control text link-url mobile_template-input" readonly/>
                <span class="input-group-addon grid-preview" id="basic-addon2">预览</span>
            </div>
        </div>
EOF
        );

        $url = Schedule::getH5Domain() ."/community/show-search-data";
        admin::script($this->addScript($url));
        $content->row($form);
        return $content;
    }

    private function addScript($url)
    {
        return <<<SCRIPT
        $('.grid-preview').unbind('click').click(function() {
            var group_names = $(".box-body.fields-group div[class^='cascade-group cascade-ab_test_title-']");
            var select_option_names = $("input[name=ab_test_title]").next().find("option");
            
            select_option_names.each(function(){
                var ab_test_title = $("input[name=ab_test_title]").next().next().find(".select2-selection__rendered").attr('title');
                if ($(this).text() == ab_test_title) {
                    $("#ab_test_id").attr("value", $(this).attr("value"));
                }
              });
            
            group_names.each(function(){
                if ($(this).attr("class").indexOf("hide") == -1) {
                    xStr = $(this).find(".select2-selection__rendered").find("span").text();
                    s = $(this).find(".select2-selection__rendered").text();
                    $("#group_name").attr("value", s.replace(xStr,''));
                }
              });

            var ab_test_id = $("#ab_test_id").attr("value");
            var group_name = $("#group_name").attr("value");
            var keyword = $("#keyword").val();
            
            if (keyword.length == 0) {
                alert("搜索词是必填的");
                throw SyntaxError(); 
            }
            
            if (!ab_test_id) {
                alert("ab实验是必填的");
                throw SyntaxError(); 
            }
            
            if (!group_name) {
                group_name = 0
            }
            
            var url = '{$url}?keyword=' + keyword
            if (ab_test_id) {
                url = url + '&ab_test_id=' + ab_test_id;
            }
            
            if (group_name) {
                url = url + '&group_name=' + group_name;
            }

            $(".mobile_template-input").val(url);
            
            layer.open({
                type: 2,
                title: '搜索结果',
                shadeClose: true,
                shade: 0,
                maxmin: true,
                area: ['700px', '600px'],
                content: $(".mobile_template-input").val(),
            });
		});
		
SCRIPT;
    }
}